package cn.easier.activity.console.repository;


import cn.easier.activity.console.entity.BargainGoods;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * 商品信息 Repository
 */
@Repository
public interface ActivityBargainRepository extends JpaRepository<BargainGoods, Integer>, JpaSpecificationExecutor<BargainGoods> {

    @Query(value = "select a.* from bargain_goods a where a.`activity_info_id` = :activityId and a.`is_del` =0 order by a.egtid desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<BargainGoods> findAllBy(@Param("activityId") Integer activityId, @Param("pageCount") int pageCount, @Param("pageSize") int pageSize);

    @Query(value = "select count(a.egtid) from bargain_goods a where a.`activity_info_id` = :activityId and a.`is_del` =0 order by a.egtid desc ", nativeQuery = true)
    long findAllByCount(@Param("activityId") Integer activityId);

    @Query(value = "UPDATE `bargain_goods` a SET a.is_del = ?  WHERE a.egtid = ? ", nativeQuery = true)
    @Modifying
    int deleteGoods(Integer status, Integer id);

    @Query(value = "SELECT * FROM `bargain_goods` a WHERE a.egtid = ? AND a.is_del = 0", nativeQuery = true)
    BargainGoods findByOne(Integer id);

    @Query(value = "UPDATE `bargain_goods` a SET a.release_flag = ? WHERE a.egtid = ? ", nativeQuery = true)
    @Modifying
    int runOrPauseOrSuperUser(Boolean release, Integer id);

    @Query(value = "select a.* from bargain_goods a where a.top_flag = ? and a.is_del = 0 and a.activity_info_id = ?",nativeQuery = true)
    BargainGoods findByTopFlag(Integer status, Integer activityId);
}
